package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;


    //保存科室信息
    @Override
    public void save(Map<String, Object> map) {
        //将map集合转换为对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);

        //检查科室是否存在
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (null != departmentExist){
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //获取分页Department部门
    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> map) {
        //获取hoscode
        String hoscode = (String)map.get("hoscode");
        //获取分页属性
        int pageNum = StringUtils.isEmpty(map.get("page"))? 1: Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit"))? 1: Integer.parseInt((String)map.get("limit"));

        //获取findAll查询函数的所有条件Pageable，ExampleMatcher
        Pageable pageable = PageRequest.of(pageNum-1,limit);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊查询
                .withIgnoreCase(true);//忽略大小写
        Department department =new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);

        Example example = Example.of(department,matcher);
        Page<Department> p = departmentRepository.findAll(example,pageable);
        return p;
    }

    //删除科室
    @Override
    public void deleteDepartment(Map<String, Object> map) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode((String) map.get("hoscode"), (String) map.get("depcode"));
        if (department != null){
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编号hoscode，查询医院所有科室的列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //声明list
        List<DepartmentVo> list = new ArrayList<>();
        Department department = new Department();
        department.setHoscode(hoscode);
        //所有科室列表
        List<Department> DeptAll = departmentRepository.findAll(Example.of(department));
        //根据大科室编号、bigcode分组、获取每个大科室里面下级子科室
        Map<String, List<Department>> collect = DeptAll.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String,List<Department>> entry: collect.entrySet()) {

            //大科室编号
            String bigcode = entry.getKey();

            //大科室编号对应全部数据list
            List<Department> DeptList = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(DeptList.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department1 : DeptList) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                children.add(departmentVo1);
            }

            //将小科室包装到大科室里
            departmentVo.setChildren(children);

            //最终返回结果，放到事先声明的list中
            list.add(departmentVo);
        }
        

        return list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }


}
